package com.coco.group.mylistener;

import com.coco.common.constant.Constant;
import com.coco.group.feign.AlipayFeign;
import com.coco.group.mapper.GroupOrderMapper;
import com.coco.group.pojo.TbGroupBuyHelp;
import com.coco.group.pojo.TbGroupOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author minicoco
 * @version 1.0
 * @date 2021/7/31 9:28
 */
@Slf4j
@Component
public class AddGroupOrderListener {

    @Autowired
    private GroupOrderMapper orderMapper;
    @Autowired
    private AlipayFeign alipayFeign;

    /**
     * 添加订单监听
     * @param order
     */
    @RabbitHandler
    @RabbitListener(queues = Constant.GroupBuyConstant.ADD_ORDER_QUEUE_ONE)
    public void addGroupOrder(TbGroupOrder order){
        log.info("我要添加订单了熬,订单号为:{}",order.getOrderId());
        orderMapper.addGroupOrder(order);
    }

    /**
     * 退款事件监听
     */
    @RabbitHandler
    @RabbitListener(queues = Constant.GroupBuyConstant.RETURN_MONEY)
    public void returnMoney(ArrayList<TbGroupBuyHelp> helpArrayList){
        //遍历进行退款
        helpArrayList.forEach(item -> {
            alipayFeign.aliReFund(item.getOrderId());
        });
    }

}
